//
//  LLNetworkTools.swift
//  新浪微博
//
//  Created by mac on 16/5/23.
//  Copyright © 2016年 mac.com. All rights reserved.
//

import UIKit
import AFNetworking
enum HTTPMethods: Int{
    case POST
    case GET
}
struct API {
    static let hostName = "https://api.weibo.com/"
    //加载token
    static let loadAccessToken = "oauth2/access_token"
    //加载用户信息
    static let loadUserInfor = "2/users/show.json"
    //加载首页数据
    static let loadHomedata = "2/statuses/home_timeline.json"

    
}

class LLNetworkTools: AFHTTPSessionManager {
    static let shardTools: LLNetworkTools = {
        let url = NSURL(string: API.hostName)
    let tools = LLNetworkTools(baseURL: url)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
               return tools
        
    }()
    func request(method: HTTPMethods,urlString:String,parameters:AnyObject?,finished:(responseObject: AnyObject?, error: NSError?) -> ()){
        let success = {
            (task:NSURLSessionDataTask,result:AnyObject?) -> () in
            finished(responseObject: result, error: nil)
    
        }
        let failure = {
            (task:NSURLSessionDataTask?,error:NSError) -> () in
            print(error)
            finished(responseObject: nil, error: error)
            
        }
        if method == .GET{
        GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        
        }else{
        
        POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
    }

    
}
///MARK: 将用户认证 获取用户信息相关的方法归到这个分类里面
extension LLNetworkTools{
    //请求用户的token
    func loadAccenToken(code:String,finished:(responseObject: AnyObject?, error: NSError?) -> ()){
    let parameters = ["client_id" : client_id, "client_secret":client_secret, "grant_type": "authorization_code","code":code,"redirect_uri":redirect_uri]
    request(.POST, urlString: API.loadAccessToken, parameters: parameters, finished: finished)
    }

//请求用户数据
    func loadUserInfor(response:[String: AnyObject],finished:(responseObject: AnyObject?, error: NSError?) -> ()){
        let token = response["access_token"]!
        let uid = response["uid"]!
        let parameters = ["access_token":token,"uid":uid]
        request(.GET, urlString: API.loadUserInfor, parameters: parameters, finished: finished)
    
    
    }
}
//首页相关
extension LLNetworkTools{
    func loadHomePageData(since_id: Int64,max_id: Int64, finished:(responseObject: AnyObject?, error: NSError?) -> ()){
        guard let token = LLUserAccountViewModel.sharedAccountViewModel.userAccount?.access_token else{
        return
        }
    var parameters = ["access_token":token,"count":"5"]
        if since_id > 0 {
            //添加since_id 到参数中
            parameters["since_id"] = "\(since_id)"
        }
        if max_id > 0 {
            //会有bug 会返回max_id 对应的微博
            parameters["max_id"] = "\(max_id - 1)"
        }
    request(.GET, urlString: API.loadHomedata, parameters: parameters, finished: finished)
    
    }
}






